﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using HAP = HtmlAgilityPack;


namespace ConsoleApp1.work7
{
    internal class FileName
    {
        class Program
        {
            // Concurrent collections to store found phone numbers and crawl history
            private static readonly ConcurrentDictionary<string, ConcurrentBag<string>> phoneUrls = new ConcurrentDictionary<string, ConcurrentBag<string>>();
            private static readonly ConcurrentBag<string> crawledUrls = new ConcurrentBag<string>();
            private static readonly HttpClient client = new HttpClient();
            private static readonly SemaphoreSlim semaphore = new SemaphoreSlim(5); // limit concurrency
            private const int TargetCount = 100;
            private static readonly Regex phoneRegex = new Regex(@"\d{3,4}-\d{7,8}", RegexOptions.Compiled);

            static async Task Main(string[] args)
            {
                Console.Write("请输入搜索关键字: ");
                string keyword = Console.ReadLine();

                int page = 1;
                while (phoneUrls.Count < TargetCount)
                {
                    // 构造搜索引擎查询（以Bing为例）
                    string searchUrl = $"https://www.bing.com/search?q={Uri.EscapeDataString(keyword)}&first={(page - 1) * 10 + 1}";
                    var searchResultHtml = await client.GetStringAsync(searchUrl);

                    // 解析搜索结果页面
                    HAP.HtmlDocument searchDoc = new HAP.HtmlDocument();
                    searchDoc.LoadHtml(searchResultHtml);
                    var links = searchDoc.DocumentNode.SelectNodes("//li[@class='b_algo']/h2/a");
                    if (links == null || links.Count == 0) break;

                    var urlList = links.Select(node => node.GetAttributeValue("href", string.Empty)).Distinct().ToList();

                    // 并行处理每个链接
                    var tasks = urlList.Select(url => ProcessUrlAsync(url));
                    await Task.WhenAll(tasks);

                    page++;
                    if (page > 10) break; // 最多翻10页
                }

                // 输出结果
                Console.WriteLine($"共找到 {phoneUrls.Count} 个电话：");
                foreach (var kv in phoneUrls.Take(TargetCount))
                {
                    Console.WriteLine($"电话: {kv.Key} 来源URL:");
                    foreach (var u in kv.Value.Distinct())
                        Console.WriteLine($"  - {u}");
                }

                Console.WriteLine("\n已爬取的URL列表：");
                foreach (var u in crawledUrls.Distinct())
                    Console.WriteLine(u);
            }

            private static async Task ProcessUrlAsync(string url)
            {
                if (phoneUrls.Count >= TargetCount) return;
                try
                {
                    await semaphore.WaitAsync();

                    // 记录已爬取URL
                    crawledUrls.Add(url);

                    string content = string.Empty;
                    try { content = await client.GetStringAsync(url); }
                    catch { return; }

                    // 提取电话
                    var matches = phoneRegex.Matches(content);
                    foreach (Match m in matches)
                    {
                        if (phoneUrls.Count >= TargetCount) break;

                        string phone = m.Value;
                        var bag = phoneUrls.GetOrAdd(phone, _ => new ConcurrentBag<string>());
                        bag.Add(url);
                    }
                }
                finally
                {
                    semaphore.Release();
                }
            }
        }
    }
}
